/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.channels;

import java.io.IOException;

import taskgraph.Config;
import taskgraph.pipes.IntInputPipe;
import taskgraph.pipes.IntOutputPipe;
import taskgraph.ports.IntInputPort;
import taskgraph.ports.IntOutputPort;


/**
 * A channel that transports {@code int} values.
 * <p>Integers are stored into, and read from, a FIFO buffer in their
 * primitive type representation without being byte-streamed.  
 * 
 * @author Armando Blancas
 */
public class IntChannel {
	
	private static final int DEFAULT_BUFFER_SIZE = Config.get().channelCapacity();
	
	private final IntOutputPipe output;
    private final IntInputPipe input;
	private final IntOutputPort outputPort;
    private final IntInputPort inputPort;
    
    /**
     * Creates a channel with the default buffer size.
     */
    public IntChannel() {
    	this(DEFAULT_BUFFER_SIZE);
    }
    
    /**
     * Creates a channel with the passed buffer size.
     * 
     * @param bufferSize The buffer capacity in bytes. 
     */
    public IntChannel(final int bufferSize) {
        if (bufferSize <= 0) {
            throw new IllegalArgumentException("IntChannel bufferSize <= 0");
         }
        
		try {
			output = new IntOutputPipe();
			input = new IntInputPipe(output, bufferSize);
			outputPort = new OutputPort();
			inputPort = new InputPort();
		} catch (IOException e) {
			throw new Error(e);
		}
    }
    
    /**
     * Gets a reference to the output port.
     * 
     * @return An output interface to this channel.
     */
    public IntOutputPort getOutputPort() {
    	return outputPort;
    }
    
    /**
     * Gets a reference to the input port.
     * 
     * @return An input interface to this channel.
     */
    public IntInputPort getInputPort() {
    	return inputPort;
    }

    /**
     * A private implementation of the int output port. It has direct access
     * to this channel and the int input port. Instances of this class are 
     * published through the IntOutputPort interface. 
     */
    private class OutputPort implements IntOutputPort {
    
		@Override
		public IntOutputPort write(int i) throws IOException {
			output.write(i);
			return this;
		}
	
		@Override
		public IntOutputPort write(int[] i) throws IOException {
			output.write(i);
			return this;
		}
	
		@Override
		public IntOutputPort write(int[] i, int off, int len) 
		throws IOException {
			output.write(i, off, len);
			return this;
		}
	
		@Override
		public void close() {
			output.close();
		}
	
    }
    
    /**
     * A private implementation of the int input port. It has direct access
     * to this channel and the int output port. Instances of this class are 
     * published through the IntInputPort interface. 
     */
    private class InputPort implements IntInputPort {

		@Override
		public int read() throws IOException {
			return input.read();
		}
	
		@Override
		public int read(int[] i) throws IOException {
			return input.read(i);
		}
	
		@Override
		public int read(int[] i, int off, int len) throws IOException {
			return input.read(i, off, len);
		}
	
		@Override
	    public final int skipInts(int n) throws IOException {
			return input.skipInts(n);
		}
	
		@Override
		public void close() {
			input.close();
		}

    }

}
